import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component:() => import('../views/LoginView.vue')
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path:'/login',
    name:'login',
    component:() => import('../views/LoginView.vue')
  },
  {
    path:'/dashboard',
    name:'dashboard',
    component:() => import('../views/HomeView.vue'),
    children: [
      {
        path: 'users',
        name: 'users',
        component: () => import('../views/UserView.vue')
      },
      {
        path: 'roles',
        name: 'roles',
        component: () => import('../views/RoleView.vue')
      },
      {
        path: 'menus',
        name: 'menus',
        component: () => import('../views/MenuView.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

//路由守卫,拦截验证功能的实现
//引入全局路由守卫 ，注册前置路由守卫
router.beforeEach((to,from,next) => {
  if(to.path === '/login' || to.path === '/' ) {
    //若是去login页面  则放行,去下一个页面
    next(); 
  } else {
    //判断是否登陆过
    //获取会话存储（或者本地存储）中存储的凭证
    const tokenValue = window.localStorage.getItem("token_key");
    // let value = store.state.isLogin;
    if(tokenValue) {
      //若浏览器存储了token  则放行
      next(); 
    }else {
      alert("还没有登录，请先登录...");
      next({path:'/login'});
    }
  }
});

export default router
